import axios from 'axios'

export default function request(config) {
	const instance = axios.create({
		baseURL: 'http://10.4.10.221:8000/',
		timeout: 5000,
	})

	// 请求拦截
	instance.interceptors.request.use(
		config => {
			return config
		},
		err => {
			console.log(err)
		}
	)

	// 响应拦截，只获取data信息
	instance.interceptors.response.use(
		res => {
			return res.data
		},
		err => {
			console.log(err)
			if (err && err.res) {
				switch (err.res.code) {
					case 400: err.message = '参数错误！'; break;
					case 401: err.message = '未经授权！'; break;
					case 500: err.message = '服务错误！'; break;
				}
			} else {
				err.message = '连接失败'
			}
			return Promise.reject(err)
		}
	)

	return instance(config)
}
